package com.mqz.zebraEcommerce.commodity.service.impl;

import com.mqz.zebraEcommerce.commodity.dao.AttrAttrGroupRelationDao;
import com.mqz.zebraEcommerce.commodity.dao.AttrGroupDao;
import com.mqz.zebraEcommerce.commodity.entity.AttrAttrGroupRelationEntity;
import com.mqz.zebraEcommerce.commodity.entity.AttrGroupEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mqz.zebraEcommerce.common.utils.PageUtils;
import com.mqz.zebraEcommerce.common.utils.Query;

import com.mqz.zebraEcommerce.commodity.dao.AttrDao;
import com.mqz.zebraEcommerce.commodity.entity.AttrEntity;
import com.mqz.zebraEcommerce.commodity.service.AttrService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Override
    public PageUtils queryBaseAttrPage(Map<String, Object> params, Long categoryId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_type", 1);
        if (categoryId != 0) {
            wrapper.eq("category_id", categoryId);
        }
        if (StringUtils.isNotBlank(key)) {
            wrapper.and(obj -> {
                obj.eq("attr_id", key).or().like("attr_name", key);
            });
        }
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public PageUtils querySaleAttrPage(Map<String, Object> params, Long categoryId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_type", 0);
        if (categoryId != 0) {
            wrapper.eq("category_id", categoryId);
        }
        if (StringUtils.isNotBlank(key)) {
            wrapper.and(obj -> {
                obj.eq("attr_id", key).or().like("attr_name", key);
            });
        }
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Resource
    private AttrGroupDao attrGroupDao;

    @Override
    public PageUtils queryValidRelationAttrPage(Map<String, Object> params, Long attrGroupId) {
        AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrGroupId);
        Long categoryId = attrGroupEntity.getCategoryId();

        List<AttrGroupEntity> groups = attrGroupDao.selectList(new QueryWrapper<AttrGroupEntity>().eq("category_id", categoryId));
        List<Long> attrGroupIds = groups.stream().map(item -> {
            return item.getId();
        }).collect(Collectors.toList());

        List<AttrAttrGroupRelationEntity> relationEntities = attrAttrGroupRelationDao.selectList(new QueryWrapper<AttrAttrGroupRelationEntity>().in("attr_group_id", attrGroupIds));
        List<Long> attrIds = relationEntities.stream().map(item -> {
            return item.getAttrId();
        }).collect(Collectors.toList());

        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<AttrEntity>().eq("category_id", categoryId).eq("attr_type", 1);

        if (attrIds.size() != 0) {
            wrapper.notIn("attr_id", attrIds);
        }

        String key = (String) params.get("key");
        if (StringUtils.isNotBlank(key)) {
            wrapper.and(obj -> {
                obj.eq("attr_id", key).or().like("attr_name", key);
            });
        }
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Resource
    private AttrAttrGroupRelationDao attrAttrGroupRelationDao;

    @Transactional
    public void saveAll(AttrEntity attrEntity) {
        this.save(attrEntity);
        if (attrEntity.getAttrType() == 1 && attrEntity.getAttrGroupId() != null) {
            AttrAttrGroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrGroupRelationEntity();
            attrAttrgroupRelationEntity.setAttrGroupId(attrEntity.getAttrGroupId());
            attrAttrgroupRelationEntity.setAttrId(attrEntity.getAttrId());
            attrAttrGroupRelationDao.insert(attrAttrgroupRelationEntity);
        }
    }

    @Transactional
    public void updateAll(AttrEntity attrEntity) {
//
        if (attrEntity.getAttrType() == 1 && attrEntity.getAttrGroupId() != null) {
            //查询关联记录
            AttrAttrGroupRelationEntity relationEntity = attrAttrGroupRelationDao.selectOne(
                    new QueryWrapper<AttrAttrGroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));
            relationEntity.setAttrGroupId(attrEntity.getAttrGroupId());
            attrAttrGroupRelationDao.updateById(relationEntity);
//           然后修改属性表
            this.updateById(attrEntity);
        }
    }

    @Transactional
    public void deleteAll(Long[] attrIds) {
        attrAttrGroupRelationDao.delete(new QueryWrapper<AttrAttrGroupRelationEntity>().in("attr_id", attrIds));
        this.removeByIds(Arrays.asList(attrIds));
    }

    @Override
    public List<AttrEntity> getRelationAttr(Long attrGroupId) {
        List<AttrAttrGroupRelationEntity> entityList = attrAttrGroupRelationDao.selectList(new QueryWrapper<AttrAttrGroupRelationEntity>().eq("attr_group_id", attrGroupId));
        List<Long> attrIds = entityList.stream().map(item -> {
            return item.getAttrId();
        }).collect(Collectors.toList());
        if (attrIds.size() == 0) {
            return null;
        }
        Collection<AttrEntity> attrEntities = this.listByIds(attrIds);
        return (List<AttrEntity>) attrEntities;
    }

    @Override
    public void deleteRelation(AttrAttrGroupRelationEntity[] attrAttrGroupRelationEntities) {
        attrAttrGroupRelationDao.deleteBatchRelation(Arrays.asList(attrAttrGroupRelationEntities));
    }
}
